{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO 1.导包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import OneHotEncoder, StandardScaler\n",
    "from sklearn.metrics import roc_auc_score, accuracy_score, classification_report\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, root_mean_squared_error\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import GridSearchCV, KFold\n",
    "from sklearn.metrics import roc_auc_score, classification_report, precision_score, recall_score, f1_score"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 2.加载数据\n",
    "train_df = pd.read_csv('../data/train.csv')\n",
    "test_df = pd.read_csv('../data/test2.csv')"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2f90a64970d6b9d6",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Age 年龄\n",
    "BusinessTravel  商务旅行情况      \n",
    "Department  部门  \n",
    "DistanceFromHome    家与工作地点的距离\n",
    "Education   教育程度    \n",
    "EducationField  教育领域    \n",
    "EmployeeNumber  员工编号    \n",
    "EnvironmentSatisfaction 工作环境满意度\n",
    "Gender  性别  \n",
    "JobInvolvement  工作投入度   \n",
    "JobLevel    工作级别    \n",
    "JobRole 工作角色    \n",
    "JobSatisfaction 工作满意度\n",
    "MaritalStatus   婚姻状况    \n",
    "MonthlyIncome   月收入 \n",
    "NumCompaniesWorked  曾工作过的公司数量   \n",
    "Over18  是否年满 18 岁\n",
    "OverTime    是否加班    \n",
    "PercentSalaryHike   薪资涨幅百分比 \n",
    "PerformanceRating   绩效评级\n",
    "RelationshipSatisfaction    人际关系满意度 \n",
    "StandardHours   标准工作时长  \n",
    "StockOptionLevel    股票期权水平\n",
    "TotalWorkingYears   总工作年限   \n",
    "TrainingTimesLastYear   去年参加培训次数    \n",
    "WorkLifeBalance 工作与生活平衡度\n",
    "YearsAtCompany  在公司工作年限 \n",
    "YearsInCurrentRole  在当前岗位工作年限   \n",
    "YearsSinceLastPromotion 自上次晋升以来的年限\n",
    "YearsWithCurrManager    与当前经理共事年限  \n",
    "Attrition   离职情况\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "dc3c2b344ae19506",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 2.特征标签确认\n",
    "# 训练集\n",
    "train_df_new = train_df.copy()\n",
    "x_train = train_df_new.drop(\n",
    "    columns=['EducationField', 'EmployeeNumber', 'Over18', 'TrainingTimesLastYear', 'StandardHours', 'Attrition'])\n",
    "y_train = train_df_new.Attrition\n",
    "\n",
    "# 测试集\n",
    "test_df_new = test_df.copy()\n",
    "x_test = test_df_new.drop(\n",
    "    columns=['EducationField', 'EmployeeNumber', 'Over18', 'TrainingTimesLastYear', 'StandardHours', 'Attrition'])\n",
    "y_test = test_df_new.Attrition"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c3ccc659a57873a8",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 3.数据处理\n",
    "# 训练集\n",
    "# 热编码\n",
    "x_train1 = pd.get_dummies(x_train)\n",
    "\n",
    "# 缺失值处理,用中位数进行替代\n",
    "for col in x_train1.columns:\n",
    "    if x_train1[col].isnull().sum() > 0:\n",
    "        x_train1[col].fillna(x_train1[col].median(), inplace=True)\n",
    "\n",
    "x_test1 = pd.get_dummies(x_test)\n",
    "\n",
    "for col in x_test1.columns:\n",
    "    if x_test1[col].isnull().sum() > 0:\n",
    "        x_test1[col].fillna(x_test1[col].median(), inplace=True)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "63dd4a3d557ec572",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 4.数据标准化\n",
    "ss = StandardScaler()\n",
    "x_train = ss.fit_transform(x_train1)\n",
    "x_test = ss.transform(x_test1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "88e12808c6aec8fe",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 5.KNN分类算法        # 最优k值 12   cv=4    分数: 0.8628571428571429\n",
    "knc = KNeighborsClassifier()\n",
    "\n",
    "# 使用校验验证网格搜索\n",
    "param_grid = {'n_neighbors': list(range(1, 30))}\n",
    "\n",
    "# 输入一个estimator, 出来一个estimator(功能变的强大)\n",
    "# for i in range(2, 11):\n",
    "#     print(f'----------- {i} ---------------------')\n",
    "#     gcv = GridSearchCV(estimator=knc, param_grid=param_grid, cv=4)\n",
    "# \n",
    "#     # 2.4 模型训练\n",
    "#     gcv.fit(x_train, y_train)\n",
    "#     y_pre = gcv.predict(x_test)\n",
    "# \n",
    "#     # 模型评估\n",
    "#     gvc_score = gcv.score(x_test, y_test)\n",
    "# \n",
    "#     # 打印超参组合\n",
    "#     print(f'最优参数组合:{gcv.best_params_}')\n",
    "#     print(f'最优分数:{gcv.best_score_}')\n",
    "# \n",
    "#     # 3.模型评估\n",
    "#     print('均方误差:', mean_squared_error(y_test, y_pre))\n",
    "#     print('均方根误差:', root_mean_squared_error(y_test, y_pre))\n",
    "#     print('平均绝对误差:', mean_absolute_error(y_test, y_pre))\n",
    "# \n",
    "#     print('分数:', gvc_score)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4dd8107c65cb02da",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# TODO 逻辑回归 \n",
    "# TODO 创建模型\n",
    "lr = LogisticRegression()\n",
    "\n",
    "# TODO 使用交叉验证网络搜索\n",
    "param_grid1 = {\n",
    "    'C': [0.01,0.02,0.03,0.04,0.05,0.06,0.07,1,5,10],\n",
    "    'penalty': ['l1', 'l2'],\n",
    "    'solver': ['liblinear']\n",
    "}\n",
    "\n",
    "# 配置5折交叉验证（shuffle=True打乱数据，避免分布不均）\n",
    "kf = KFold(n_splits=5, shuffle=True, random_state=42)\n",
    "\n",
    "# 创建网格搜索对象\n",
    "gcv = GridSearchCV(\n",
    "    estimator=lr,  # 要优化的模型\n",
    "    param_grid=param_grid1,  # 超参数网格\n",
    "    cv=kf,  # 交叉验证方式\n",
    "    scoring='accuracy',  # 评估指标（可选'roc_auc'、'f1'等）\n",
    "    n_jobs=-1,  # 并行计算（-1表示使用所有CPU核心）\n",
    "    verbose=1  # 输出调参过程信息（0=不输出，1=简要，2=详细）\n",
    ")\n",
    "\n",
    "# 模型训练\n",
    "gcv.fit(x_train, y_train)\n",
    "y_pre = gcv.predict(x_test)\n",
    "\n",
    "# 模型评估\n",
    "gvc_score = gcv.score(x_test, y_test)\n",
    "\n",
    "# 打印超参组合\n",
    "print(f'最优参数组合:{gcv.best_params_}')\n",
    "print(f'最优分数:{gcv.best_score_}')\n",
    "\n",
    "# 3.模型评估\n",
    "print('均方误差:', mean_squared_error(y_test, y_pre))\n",
    "print('均方根误差:', root_mean_squared_error(y_test, y_pre))\n",
    "print('平均绝对误差:', mean_absolute_error(y_test, y_pre))\n",
    "\n",
    "print('分数:', gvc_score)\n",
    "\n",
    "# TODO 准确率\n",
    "print('准确率:', precision_score(y_test, y_pre))\n",
    "\n",
    "# TODO 精确率\n",
    "print('精确率:', recall_score(y_test, y_pre))\n",
    "\n",
    "# TODO 召回率\n",
    "print('召回率:', f1_score(y_test, y_pre))\n",
    "\n",
    "# TODO AUC指标_分类评估报告\n",
    "cr = classification_report(y_test, y_pre, labels=[0, 1], target_names=None)\n",
    "print('AUC指标_分类评估报告:', cr)\n",
    "\n",
    "# TODO roc曲线\n",
    "y_proba = lr.predict_proba(x_test)[:, 1]\n",
    "print(\"roc曲线:\", roc_auc_score(y_test, y_proba))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "849822f61522880d",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "921c7d2b5d79eeaf",
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
